package com.enniswu.weitter.user.start;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BasePresenter;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.data.remote.UserLoader;
import com.enniswu.weitter.entity.User;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StartPresenter extends BasePresenter<StartContract.View> implements StartContract.Presenter {

    private StartModel sModel;
    private UserLoader uLoader;
    private String phone;
    private String password;

    public StartPresenter(StartContract.View view) {
        super(view);
        this.sModel = new StartModel();
        this.uLoader = new UserLoader();
    }

    @Override
    public void login(String phone, String password) {
        loginOrRegister(StartModel.TYPE_LOGIN, phone, password);
    }

    @Override
    public void register(String phone, String password, String code) {
        if (!phone.equals(this.phone)) {
            view.showMessage(R.string.start_not_send_code);
            return;
        }
        this.password = password;
        view.showLoading();
        authCode(code);
    }

    @Override
    public void sendCode(String phone) {
        if (!isPhone(phone))
            return;
        this.phone = phone;
        sModel.sendCode(phone, new AbstractCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {

            }
        });
    }

    private void authCode(String code) {
        String fromTime = SimpleDateFormat.getDateTimeInstance().format(new Date());
        sModel.authCode(phone, code, fromTime, new AbstractCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {
                if (view == null)
                    return;
                loginOrRegister(StartModel.TYPE_REGISTER, phone, password);
            }
        });
    }

    private void loginOrRegister(final int type, String phone, String password) {
        if (!isPhone(phone))
            return;
        view.showLoading();
        sModel.loginOrRegister(type, phone, password, new AbstractCallback<Integer>() {
            @Override
            public void onSuccess(@Nullable Integer o) {
                if (!handleNoNullSuccess(o))
                    return;
                if (type == StartModel.TYPE_LOGIN) {
                    getUser(o);
                } else {
                    view.next(o);
                }
            }
        });
    }

    private void getUser(int id) {
        uLoader.getInfo(id, new AbstractCallback<User>() {
            @Override
            public void onSuccess(@Nullable User user) {
                if (!handleNoNullSuccess(user))
                    return;
                DBSupport.getInstance().setCurrentUser(user);
                SPSupport.getInstance().putId(user.getId());
                EventBus.getDefault().post(user);
                view.dismissLoading();
                view.next(user.getId());
            }
        });
    }

    private boolean isPhone(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11) {
            view.showMessage(R.string.start_enter_error_phone);
            return false;
        }
        return true;
    }
}
